﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes {
    import Engine.Classes.*;
    import flash.events.*;
    import flash.display.*;
    import flash.geom.*;
    import Engine.Helpers.*;
    import Classes.sim.*;
    import com.greensock.*;
    import Display.FactoryUI.*;
    import Engine.*;

	/**
	 * 地图上小人的基类
	 */
    public class NPC extends MapResource {

        private const NAV_WOBBLE:Number = 0.1;
        private const NAV_RIGHT_PROB:Number = 0.5;
        private const MIN_AMBIENT_DELTA:Number = 10000;
        private const NPCTYPE:String = "npc";

        private var m_feedback:Sprite;
        private var m_feedbackEnd:Number = -1;
        private var m_performsAmbientActions:Boolean = false;
        private var m_nextAmbientActionStart:Number = 0;
        private var m_velocityWalk:Number = 0;
        private var m_velocityRun:Number = 0;
        private var m_roadOffset:Number = 0;
        private var m_isShip:Boolean = false;
        private var m_animationState:String = "static";
        private var m_actionSelection:IActionSelection;
        private var m_isFreeAgent:Boolean = true;
        private var m_isRightSideNav:Boolean = true;
        protected var m_homeId:int = 0;
        protected var m_slidePick:SlidePick = null;
        private var m_lastFeedbackPosition:Point;
        private var m_feedbackRendered:Boolean = false;
        private var m_feedbackRect:Rectangle;
        private var m_feedbackDirtyRect:Rectangle;
        private var m_navDbgFeedback:Shape = null;
        public var m_navDbgPath:Array;
        public var m_navDbgPath2:Array;
        public var m_navRandom:Boolean = false;
        protected var m_alwaysCulled:Boolean = false;
        protected var m_playActionCallback:Function = null;
        protected var m_tooltipHeaderGenerator:Function = null;
        protected var m_tooltipStatusGenerator:Function = null;
        protected var m_imageLoadedCallback:Function = null;
        protected var m_onHighlightCallback:Function = null;

        public function NPC(_arg1:String, _arg2:Boolean, _arg3:Number=-1){
            this.m_lastFeedbackPosition = new Point();
            this.m_feedbackRect = new Rectangle();
            this.m_feedbackDirtyRect = new Rectangle();
            super(_arg1);
            m_objectType = WorldObjectTypes.NPC;
            m_typeName = this.NPCTYPE;
            if (_arg3 >= 0){
                this.m_velocityWalk = _arg3;
            } else {
                this.m_velocityWalk = MathUtil.randomWobble(m_item.navigateXml.walkSpeed, this.NAV_WOBBLE);
            };
            this.m_velocityRun = MathUtil.randomWobble(m_item.navigateXml.runSpeed, this.NAV_WOBBLE);
            this.m_roadOffset = m_item.navigateXml.roadOffset;
            this.m_isShip = (m_item.navigateXml.isShip == true);
            m_collisionFlags = Constants.COLLISION_NONE;
            this.m_isFreeAgent = _arg2;
            m_isHighlightable = false;
            if (!this.isVehicle){
                this.m_roadOffset = MathUtil.randomWobble(this.m_roadOffset, this.NAV_WOBBLE);
                this.m_isRightSideNav = (Math.random() < this.NAV_RIGHT_PROB);
            };
            this.m_actionSelection = this.makeActionSelection();
            this.randomizeNextAmbientActionStart();
            var _local4:Boolean = Global.player.npc_cloud_visible;
            if (!_local4){
                this.m_alwaysCulled = true;
            };
        }
        override public function attach():void{
            super.attach();
        }
        override public function detach():void{
            this.m_navDbgPath = null;
            this.drawNavFeedback();
            this.hideFeedbackBubble();
            this.getStateMachine().removeAllStates();
            this.hideSlidePick();
            super.detach();
        }
        public function setHome(_arg1:Residence):void{
            this.m_homeId = _arg1.getId();
        }
        public function getHome():Residence{
            if (this.m_homeId == 0){
                return (null);
            };
            return ((Global.world.getObjectById(this.m_homeId) as Residence));
        }
        public function get alwaysCulled():Boolean{
            return (this.m_alwaysCulled);
        }
        public function set alwaysCulled(_arg1:Boolean):void{
            this.m_alwaysCulled = _arg1;
        }
        override public function isWithinViewport():Boolean{
            return (((!(this.m_alwaysCulled)) && (super.isWithinViewport())));
        }
        override protected function calculateDepthIndex():void{
            if (this.m_alwaysCulled){
                m_depthIndex = 0;
            } else {
                return (super.calculateDepthIndex());
            };
        }
        public function get isVehicle():Boolean{
            return (false);
        }
        public function get isSeaVehicle():Boolean{
            return (false);
        }
        public function get isCloud():Boolean{
            return (false);
        }
        public function get playActionCallback():Function{
            return (this.m_playActionCallback);
        }
        public function set playActionCallback(_arg1:Function):void{
            this.m_playActionCallback = _arg1;
            this.m_isHighlightable = !((_arg1 == null));
        }
        override public function onPlayAction():void{
            super.onPlayAction();
            if (this.m_playActionCallback != null){
                this.m_playActionCallback();
            };
        }
        protected function makeActionSelection():IActionSelection{
            return (new NPCActionSelection(this));
        }
        public function get slidePick():SlidePick{
            return (this.m_slidePick);
        }
        public function set slidePick(_arg1:SlidePick):void{
            this.m_slidePick = _arg1;
        }
        public function showSlidePick():void{
            if (this.m_slidePick){
                GlobalEngine.viewport.hudBase.addChild(this.m_slidePick);
                this.m_slidePick.setPosition(m_position.x, m_position.y);
            };
        }
        public function hideSlidePick(_arg1:Number=0):void{
            var tweenTimeSeconds:int = _arg1;
            if ((((((tweenTimeSeconds == 0)) && (this.m_slidePick))) && (this.m_slidePick.parent))){
                this.m_slidePick.parent.removeChild(this.m_slidePick);
            } else {
                if (((this.m_slidePick) && (this.m_slidePick.parent))){
                    TweenLite.to(this.m_slidePick, tweenTimeSeconds, {
                        alpha:0,
                        onComplete:function (... _args):void{
                            if (((m_slidePick) && (m_slidePick.parent))){
                                m_slidePick.parent.removeChild(m_slidePick);
                                m_slidePick.alpha = 1;
                            };
                        }
                    });
                };
            };
        }
        public function isSlidePickShown():Boolean{
            return (((!((this.m_slidePick == null))) && (!((this.m_slidePick.parent == null)))));
        }
        public function get isRightSideNav():Boolean{
            return (this.m_isRightSideNav);
        }
        override protected function getLayerName():String{
            if (Game.m_blitting == true){
                return ("npc");
            };
            return (null);
        }
        public function get isShowingFeedback():Boolean{
            return (!((this.m_feedback == null)));
        }
        public function get isFreeAgent():Boolean{
            return (this.m_isFreeAgent);
        }
        public function set isFreeAgent(_arg1:Boolean):void{
            this.m_isFreeAgent = _arg1;
        }
        public function set performsAmbientActions(_arg1:Boolean):void{
            this.m_performsAmbientActions = _arg1;
        }
        public function get performsAmbientActions():Boolean{
            return (this.m_performsAmbientActions);
        }
        public function get nextAmbientActionStart():Number{
            return (this.m_nextAmbientActionStart);
        }
        public function getStateMachine():NPCActionQueue{
            return (m_actionQueue);
        }
        public function get actionSelection():IActionSelection{
            return (this.m_actionSelection);
        }
        public function set actionSelection(_arg1:IActionSelection):void{
            this.m_actionSelection = _arg1;
        }
        public function set tooltipHeaderGenerator(_arg1:Function):void{
            this.m_tooltipHeaderGenerator = _arg1;
        }
        public function set tooltipStatusGenerator(_arg1:Function):void{
            this.m_tooltipStatusGenerator = _arg1;
        }
        public function randomizeNextAmbientActionStart():void{
            this.m_nextAmbientActionStart = (GlobalEngine.getTimer() + Math.floor(((1 + Math.random()) * this.MIN_AMBIENT_DELTA)));
        }
        override public function setPosition(_arg1:Number, _arg2:Number, _arg3:Number=0):void{
            super.setPosition(_arg1, _arg2, _arg3);
        }
        public function showFeedbackBubble(_arg1:DisplayObject, _arg2:Number=-1):void{
            if (_arg2 > 0){
                this.m_feedbackEnd = (GlobalEngine.getTimer() + int((_arg2 * 1000)));
            } else {
                this.m_feedbackEnd = -1;
            };
            this.m_feedback = this.createFeedbackBubble(_arg1);
            TweenLite.from(this.m_feedback, 0.3, {alpha:0});
            m_arrow = this.m_feedback;
            setState(getState());
            this.conditionallyReattach();
        }
        public function clearStates():void{
            m_actionQueue.removeAllStates();
        }
        override public function setHighlighted(_arg1:Boolean, _arg2:uint=16755200):void{
            super.setHighlighted(_arg1, _arg2);
            if (this.m_onHighlightCallback != null){
                this.m_onHighlightCallback(_arg1, m_highlighted);
            };
        }
        public function set highlightCallback(_arg1:Function):void{
            this.m_onHighlightCallback = _arg1;
        }
        public function get highlightCallback():Function{
            return (this.m_onHighlightCallback);
        }
        public function set imageLoadedCallback(_arg1:Function):void{
            this.m_imageLoadedCallback = _arg1;
        }
        public function get imageLoadedCallback():Function{
            return (this.m_imageLoadedCallback);
        }
        protected function createFeedbackBubble(_arg1:DisplayObject):Sprite{
            var _local2:Sprite = new Sprite();
            _local2.addChild(new EmbeddedArt.npcThoughtBubble());
            _local2.addChild(_arg1);
            return (_local2);
        }
        private function onFeedbackAssetLoaded(_arg1:Event):void{
            if (m_outer != null){
                this.m_feedback = this.createFeedbackBubble((_arg1.target as LoaderInfo).content);
                TweenLite.from(this.m_feedback, 0.3, {alpha:0});
                m_arrow = this.m_feedback;
                setState(getState());
                this.conditionallyReattach();
            };
        }
        public function hideFeedbackBubble():void{
            if (((this.m_feedback) && (m_arrow))){
                TweenLite.to(this.m_feedback, 0.3, {
                    alpha:0,
                    onComplete:this.finishHideFeedback
                });
            };
        }
        private function finishHideFeedback():void{
            this.m_feedback = null;
            m_arrow = null;
            if (m_outer != null){
                setState(getState());
                this.conditionallyReattach();
            };
        }
        public function get velocityWalk():Number{
            return (this.m_velocityWalk);
        }
        public function get velocityRun():Number{
            return (this.m_velocityRun);
        }
        public function set velocityWalk(_arg1:Number):void{
            this.m_velocityWalk = _arg1;
        }
        public function set velocityRun(_arg1:Number):void{
            this.m_velocityRun = _arg1;
        }
        public function get roadOffset():Number{
            return (this.m_roadOffset);
        }
        override public function setItem(_arg1:Item):void{
            super.setItem(_arg1);
            m_size = new Vector3(0.01, 0.01);
        }
        override public function getToolTipHeader():String{
            if (this.m_tooltipHeaderGenerator == null){
                return (null);
            };
            return (this.m_tooltipHeaderGenerator());
        }
        override public function getToolTipStatus():String{
            if (this.m_tooltipStatusGenerator == null){
                return (null);
            };
            return (this.m_tooltipStatusGenerator());
        }
        public function set animation(_arg1:String):void{
            this.m_animationState = _arg1;
            setState(getState());
        }
        public function get animation():String{
            return (this.m_animationState);
        }
        override protected function getCurrentImage():ItemImageInstance{
            var _local1:String = this.m_animationState;
            var _local2:ItemImageInstance = m_item.getCachedImage(_local1, this, m_direction);
            return (_local2);
        }
        public function drawNavFeedback():void{
            var _local1:Vector3;
            var _local2:Point;
            var _local3:Number;
            var _local4:PathElement;
            var _local5:Vector3;
            if (!this.m_navDbgPath){
                if (this.m_navDbgFeedback){
                    GlobalEngine.viewport.objectBase.removeChild(this.m_navDbgFeedback);
                    this.m_navDbgFeedback = null;
                };
                return;
            };
            if (!this.m_navDbgFeedback){
                this.m_navDbgFeedback = new Shape();
                GlobalEngine.viewport.objectBase.addChild(this.m_navDbgFeedback);
            };
            this.m_navDbgFeedback.graphics.clear();
            if (((this.m_navDbgPath) && ((this.m_navDbgPath.length > 0)))){
                this.m_navDbgFeedback.graphics.lineStyle(0.5, 0xFF0000);
                _local1 = getPosition();
                _local2 = IsoMath.tilePosToPixelPos(_local1.x, _local1.y);
                this.m_navDbgFeedback.graphics.moveTo((_local2.x - 1), _local2.y);
                this.m_navDbgFeedback.graphics.lineTo((_local2.x + 1), _local2.y);
                this.m_navDbgFeedback.graphics.moveTo(_local2.x, _local2.y);
                this.m_navDbgFeedback.graphics.lineTo(_local2.x, (_local2.y - 1));
                this.m_navDbgFeedback.graphics.lineTo(_local2.x, (_local2.y + 1));
                this.m_navDbgFeedback.graphics.moveTo(_local2.x, _local2.y);
                _local3 = 0;
                while (_local3 < this.m_navDbgPath.length) {
                    _local4 = this.m_navDbgPath[_local3];
                    _local5 = _local4.offsetPosition;
                    _local2 = IsoMath.tilePosToPixelPos(_local5.x, _local5.y);
                    if (this.m_navRandom){
                        this.m_navDbgFeedback.graphics.lineStyle(0.5, 0xFF00FF);
                    } else {
                        this.m_navDbgFeedback.graphics.lineStyle(0.5, 0xFFFF);
                    };
                    this.m_navDbgFeedback.graphics.lineTo(_local2.x, _local2.y);
                    this.m_navDbgFeedback.graphics.lineStyle(0.5, 0xFF0000);
                    this.m_navDbgFeedback.graphics.moveTo((_local2.x - 1), _local2.y);
                    this.m_navDbgFeedback.graphics.lineTo((_local2.x + 1), _local2.y);
                    this.m_navDbgFeedback.graphics.moveTo(_local2.x, _local2.y);
                    this.m_navDbgFeedback.graphics.lineTo(_local2.x, (_local2.y - 1));
                    this.m_navDbgFeedback.graphics.lineTo(_local2.x, (_local2.y + 1));
                    this.m_navDbgFeedback.graphics.moveTo(_local2.x, _local2.y);
                    _local3 = (_local3 + 1);
                };
            };
        }
        override protected function onItemImageLoaded(_arg1:Event):void{
            super.onItemImageLoaded(_arg1);
            if (this.m_imageLoadedCallback != null){
                this.m_imageLoadedCallback();
            };
        }
        override public function conditionallyReattach(_arg1:Boolean=false):void{
            if (isAttached() == false){
                this.attach();
            } else {
                if (((((((_arg1) || (m_transformDirty))) || (m_displayObjectDirty))) || ((m_attachPosition == null)))){
                    if (!this.m_alwaysCulled){
                        if (m_displayObjectDirty){
                            drawDisplayObject();
                        } else {
                            updateDisplayObjectTransform();
                        };
                        this.calculateDepthIndex();
                        m_outer.updateObjectInDepthArray(this);
                    };
                    updateCulling();
                    m_attachPosition = m_position.clone();
                    m_attachSize = m_size.clone();
                    m_transformDirty = false;
                };
            };
        }
        public function calcDirtyRect(_arg1:RenderContext):Rectangle{
            var _local3:Rectangle;
            var _local4:CompositeItemImage;
            var _local2:Point = new Point();
            if (m_lastScreenPosition){
                _local2 = _arg1.transformMatrix.transformPoint(m_lastScreenPosition);
                if (m_imageClass){
                    _local4 = (m_imageClass.image as CompositeItemImage);
                    if (_local4){
                        m_dirtyRect = _local4.getDirtyRect();
                        m_dirtyRect.x = (m_dirtyRect.x + _local2.x);
                        m_dirtyRect.y = (m_dirtyRect.y + _local2.y);
                    };
                };
                _local3 = this.calculateFeedbackDirtyRect(_arg1, _local2);
                m_dirtyRect = m_dirtyRect.union(_local3);
            } else {
                m_lastScreenPosition = new Point();
            };
            return (m_dirtyRect.clone());
        }
        protected function calculateFeedbackDirtyRect(_arg1:RenderContext, _arg2:Point):Rectangle{
            var _local3:Number;
            var _local4:Number;
            if (this.m_feedbackRendered){
                this.m_feedbackDirtyRect.width = this.m_feedbackRect.width;
                this.m_feedbackDirtyRect.height = this.m_feedbackRect.height;
                _local3 = (m_displayObject.scaleX * _arg1.transformMatrix.a);
                _local4 = (m_displayObject.scaleY * _arg1.transformMatrix.d);
                this.m_feedbackDirtyRect.x = (_arg2.x + (_local3 * this.m_lastFeedbackPosition.x));
                this.m_feedbackDirtyRect.y = (_arg2.y + (_local3 * this.m_lastFeedbackPosition.y));
                this.m_feedbackDirtyRect.x = int(this.m_feedbackDirtyRect.x);
                this.m_feedbackDirtyRect.y = int(this.m_feedbackDirtyRect.y);
            } else {
                this.m_feedbackDirtyRect.setEmpty();
            };
            return (this.m_feedbackDirtyRect.clone());
        }
        override public function render(_arg1:RenderContext):void{
            var _local4:Number;
            var _local5:Number;
            var _local6:CompositeItemImage;
            var _local2:Point = new Point();
            var _local3:Point = new Point();
            _local2 = _arg1.transformMatrix.transformPoint(m_screenPosition);
            if (m_lastScreenPosition){
                _local3 = _arg1.transformMatrix.transformPoint(m_lastScreenPosition);
            } else {
                m_lastScreenPosition = new Point();
                _local3.x = _local2.x;
                _local3.y = _local2.y;
            };
            if (m_imageClass){
                _local6 = (m_imageClass.image as CompositeItemImage);
                if (_local6){
                    _local4 = (m_displayObject.scaleX * _arg1.transformMatrix.a);
                    _local5 = (m_displayObject.scaleY * _arg1.transformMatrix.d);
                    if (_arg1.targetBuffer){
                        _local6.renderCiToBuffer(_arg1, _local4, _local5, _local3, _local2, this.alpha);
                    };
                };
                this.renderFeedback(_arg1, _local3, _local2);
            };
            m_lastScreenPosition.x = m_screenPosition.x;
            m_lastScreenPosition.y = m_screenPosition.y;
        }
        protected function renderFeedback(_arg1:RenderContext, _arg2:Point, _arg3:Point):void{
            var _local6:Number;
            var _local7:Number;
            var _local8:int;
            var _local9:DisplayObject;
            var _local10:Bitmap;
            var _local4:Point = _arg2.clone();
            var _local5:Point = _arg3.clone();
            if (this.m_feedback){
                this.m_lastFeedbackPosition.x = this.m_feedback.x;
                this.m_lastFeedbackPosition.y = this.m_feedback.y;
                _local6 = (m_displayObject.scaleX * _arg1.transformMatrix.a);
                _local7 = (m_displayObject.scaleY * _arg1.transformMatrix.d);
                _local4.x = (_local4.x + (_local6 * this.m_feedback.x));
                _local4.y = (_local4.y + (_local7 * this.m_feedback.y));
                _local5.x = (_local5.x + (_local6 * this.m_feedback.x));
                _local5.y = (_local5.y + (_local7 * this.m_feedback.y));
                this.m_feedbackRect.x = int(_local4.x);
                this.m_feedbackRect.y = int(_local4.y);
                this.m_feedbackRect.width = this.m_feedback.width;
                this.m_feedbackRect.height = this.m_feedback.height;
                _local4.x = int(_local4.x);
                _local4.y = int(_local4.y);
                _local8 = 0;
                while (_local8 < this.m_feedback.numChildren) {
                    _local9 = this.m_feedback.getChildAt(_local8);
                    _local10 = (_local9 as Bitmap);
                    if (_local10){
                        this.m_feedbackRendered = true;
                        this.m_feedbackRect = _local10.bitmapData.rect.clone();
                        this.m_feedbackRect.x = int(_local4.x);
                        this.m_feedbackRect.y = int(_local4.y);
                        if (this.alpha > 0.2){
                            if (_arg1.alphaBuffer){
                                _arg1.targetBuffer.copyPixels(_local10.bitmapData, _local10.bitmapData.rect, _local5, _arg1.alphaBuffer, _local5, false);
                            } else {
                                _arg1.targetBuffer.copyPixels(_local10.bitmapData, _local10.bitmapData.rect, _local5);
                            };
                        };
                    };
                    _local8++;
                };
            };
        }
        override public function onUpdate(_arg1:Number):void{
            super.onUpdate(_arg1);
            this.drawNavFeedback();
            this.updateActionQueue(_arg1);
            if (((!((this.m_feedback == null))) && ((this.m_feedbackEnd >= 0)))){
                if (GlobalEngine.getTimer() > this.m_feedbackEnd){
                    this.m_feedbackEnd = -1;
                    this.hideFeedbackBubble();
                };
            };
            if (this.m_slidePick != null){
                this.m_slidePick.setPosition(m_position.x, m_position.y);
            };
        }
        protected function updateActionQueue(_arg1:Number):void{
            var _local2:Array;
            if (m_actionQueue.getState() == null){
                _local2 = this.m_actionSelection.getNextActions();
                if (((!((_local2 == null))) && ((_local2.length > 0)))){
                    m_actionQueue.addActionsArray(_local2);
                };
            };
        }
        override public function canBeDragged():Boolean{
            return (false);
        }
        override public function showObjectBusy():void{
        }

    }
}//package Classes 
